//
//  ScenicCollectViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "ScenicCollectViewController.h"
#import "ScenicDetailModel.h"
#import "ScenicDatabaseManager.h"
#import "ScenicCollectDetailViewController.h"

@interface ScenicCollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation ScenicCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self setUpData];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self addSubViews];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self setUpData];
    self.navigationController.navigationBar.hidden = NO;
    [self.tableView reloadData];
}


#pragma mark -- 设置导航栏

- (void)setUpData
{
     self.dataArray = [[ScenicDatabaseManager shareManager] selectAllModels];
}

- (void)addSubViews
{
    UIImageView *backgroundImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    backgroundImg.image = [UIImage imageNamed:@"MineBackGround.jpg"];
    backgroundImg.userInteractionEnabled = YES;
    [self.view addSubview:backgroundImg];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    self.tableView.dataSource = self;
    self.tableView.delegate= self;
    self.tableView.alpha = 0.8;
    [backgroundImg addSubview:self.tableView];
}

#pragma mark -- UITableViewDataSource协议


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    if (self.dataArray.count > 0) {

        ScenicDetailModel *model = self.dataArray[indexPath.row];
        cell.textLabel.text =[NSString stringWithFormat:@"%@", model.Name];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"     %@", model.Foreword];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark -- 跳转界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 选中cell后不变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
         ScenicDetailModel *model = self.dataArray[indexPath.row];
        ScenicCollectDetailViewController *scVC = [[ScenicCollectDetailViewController alloc] init];
        scVC.model = model;
        [self.navigationController pushViewController:scVC animated:YES];

}

// 编辑
  // 开启编辑状态
  // 返回编辑样式
  // 完成编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 数据库中删除数据
    ScenicDetailModel *model = self.dataArray[indexPath.row];
    [[ScenicDatabaseManager shareManager] deleteByName:model.Name];
    // 数组中删除数据
    [self.dataArray removeObject:model];
    // 刷新列表
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
